<script lang="ts" setup>
import { onMounted, reactive } from 'vue'
import { User, Lock } from '@element-plus/icons-vue';
import { useUserStore } from '../../store/modules/user';
import { ref } from 'vue';
//@ts-ignore
import { ElNotification } from "element-plus";
import { theDate } from '../../utils/date'
import { useRoute, useRouter } from 'vue-router';
let $route = useRoute();
let $router = useRouter();


let loading = ref(false);
let useUser = useUserStore();
let loginForm = reactive({ username: 'admin', password: 'atguigu123' });



// 获取表单验证元素
let loginForms = ref();

const login = async () => {
    await loginForms.value.validate();
    //加载效果:开始加载
    loading.value = true;
    try {
        let res = await useUser.useLogin(loginForm);

        let red:any = $route.query.redirect
        if (!red) {
            $router.push("/");
        } else {
            $router.push({
                path:red
            })
        }
        ElNotification({
            title: "登陆成功",
            type: "success",
            message: `欢迎回来,${theDate()}`,
        });
    } catch (error: any) {
        ElNotification({
            title: "登陆失败",
            type: "error",
            message: error,
        });
        loading.value = false;

    }

}
// 校验
// 自定义校验规则
// rule 要校验的对象 value 表单内容 callback 校验成功失败返回的函数
const validateUserName = (rule: any, value: any, callback: any) => {
    if (value.length >= 5) {
        callback();
    } else {
        callback(new Error('请输入至少五位账号'));
    }
}

const validatePassWord = (rule: any, value: any, callback: any) => {
    if (value.length >= 6) {
        callback();
    } else {
        callback(new Error('请输入至少六位密码'));
    }
}

// 1.:model 绑定数据 2.:rules 绑定规则 3.prop 规则命名 4.写规则
const loginRules = {
    username: [
        {
            trigger: 'change',
            validator: validateUserName
        }
    ],
    password: [
        {
            trigger: 'change',
            validator: validatePassWord
        }
    ],
}



</script>

<template>
    <div class="login_container">
        <el-row>
            <!-- 左右分隔 12 小于760左边替换为右边 -->
            <el-col :span="12" :xs="0"></el-col>
            <el-col :span="12" :xs="24">
                <el-form class="login_form" :model="loginForm" :rules="loginRules" ref="loginForms">
                    <h1>Hello</h1>
                    <h2>欢迎来到硅谷甄选</h2>
                    <!-- 账户 -->
                    <el-form-item prop="username">
                        <el-input placeholder="请输入用户名" v-model="loginForm.username" :prefix-icon="User"></el-input>
                    </el-form-item>
                    <!-- 密码 -->
                    <el-form-item prop="password">
                        <el-input type="password" v-model="loginForm.password" show-password :prefix-icon="Lock"></el-input>
                    </el-form-item>
                    <!-- 登录 -->
                    <el-form-item>
                        <el-button @click="login" :loading="loading" class="login_btn" type="primary"
                            color="blue">登录</el-button>
                    </el-form-item>
                </el-form>
            </el-col>
        </el-row>

    </div>
</template>

<style lang="scss" scoped>
.login_container {
    width: 100%;
    height: 100vh;
    background: url('@/assets/images/background.jpg');
    background-size: cover;

    .login_form {
        position: relative;
        width: 80%;
        top: 30vh;
        background: url("@/assets/images/login_form.png") no-repeat;
        background-size: cover;
        padding: 40px;

        h1 {
            color: white;
            font-size: 40px;
        }

        h2 {
            font-size: 20px;
            color: white;
            margin: 20px 0px;
        }

        .login_btn {
            width: 100%;
        }
    }
}
</style>